package ClientDatabaseServices;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.fabric.xmlrpc.Client;

public class AuthenticationDb{
	private String url = "jdbc:mysql://127.0.0.1:3306/";
	private String dbName = "LocalDatabase";
	private String driver = "com.mysql.jdbc.Driver";
	private String userName = "root";
	private String password = "cnncnn22";
	private Connection conn;
	private Statement st;
	private ResultSet res;
	private PreparedStatement ps;
	private boolean printMsg = true;
	
	public AuthenticationDb(){
		connect();
	}
	
	private void connect() {
		if (printMsg) {
			System.out.print("Trying to connect to database . . . ");
		}
		try {
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(url + dbName, userName, password);
			st = conn.createStatement();
			if (printMsg) {
				System.out.println("Connected!!!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void disconnect() {
		try {
			conn.close();
			st.close();
			if (printMsg) {
				System.out.println("Disconnected from database.");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getAuthenticationResult(ArrayList loginList){
		String username=loginList.get(1).toString();
		String password=loginList.get(2).toString();
		System.out.println("userName: " + username);
		System.out.println("password: " + password);
		return databaseAuthentication(username,password);
	}
    
	private String databaseAuthentication(String userssn,String password){
		System.out.println("\n\n\nWE ARE IN THe: databaseAuthentication METHOD \n\n\n");
		String returnResult="error_Authentication FAILD_";
		String authenticationString=null;
		String query="SELECT room.id, room.name FROM room,user WHERE user.SSN='"+userssn+"' && user.password='"+password+"'; ";
		ArrayList authenticationList = new ArrayList();
		try {
				ps = conn.prepareStatement(query);
				res = ps.executeQuery();
				while(res.next()){
					authenticationList.add(res.getString("id"));
					authenticationList.add(res.getString("name"));
				}
				for(int q=0;q<authenticationList.size();q++){
					System.out.println("authenticationList.get("+q+") == "+authenticationList.get(q));
				}
				if(authenticationList.size()>0){
					System.out.println("User "+userssn+" has Succesfully been Authenticated ");
					returnResult= "authenticate_"+userssn+"_"+convertArrayListToString(authenticationList);
				}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		System.out.println("\n#####################\n#####################\n#####################\n#####################\n#####################\n#####################\n#####################\nreturn "+ returnResult+";\n#####################\n#####################\n#####################\n#####################\n#####################\n#####################\n#####################\n");
		return returnResult;
		/*
		  -------------------------------------------------------
		  Wich repositories is most sutable for programming students? 
		  
		  How to simplyfy teamwork in programming among students?
		  
		  How to better integrate a student suitable reposotory in 
		  eudaction  laboration praxis?
		  --------------------------------------------------------
		  */
	}
	
	private ArrayList convertStringToArrayList(String stringList) {
		ArrayList taskList = new ArrayList();
		int lastSeparator = -1;
		for (int i = 0; i < stringList.length(); i++) {
			if (stringList.charAt(i) == '_') {
				taskList.add(stringList.subSequence(lastSeparator + 1, i));
				lastSeparator = i;
			}
		}
		return taskList;
	}

	private String convertArrayListToString(ArrayList arrayList) {
		String stringList = "";
		int lastSeparator = -1;
		for (int i = 0; i < arrayList.size(); i++) {
			stringList = stringList + arrayList.get(i) + "_";
		}
		return stringList;
	}
}   